package com.lixingqu.mvpdemo.ui.news;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.SimpleLoadMoreView;
import com.lixingqu.mvpdemo.R;
import com.lixingqu.mvpdemo.entity.NewsEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lixingqu on 2016/12/2.
 */

public class NewsActivity extends Activity implements NewsContract.View, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    private NewsContract.Presenter mPresenter;
    private NewsAdapter adapter;
    @BindView(R.id.sr_activity_news)
    SwipeRefreshLayout mSwipeRefresh;
    @BindView(R.id.rv_activity_news_content)
    RecyclerView mRecyclerView;

    private List<NewsEntity> list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        ButterKnife.bind(this);
        mPresenter = new NewsPresenterImpl(this, new NewsModelImpl());
        mSwipeRefresh.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mSwipeRefresh.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        list = new ArrayList<>();
        initAdapter();
        mPresenter.onRefresh();
    }

    private void initAdapter() {
        adapter = new NewsAdapter(list);
        adapter.setOnLoadMoreListener(this);
        adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        adapter.isFirstOnly(true);
        adapter.setLoadMoreView(new SimpleLoadMoreView());
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
    }


    @Override
    public void refresh(final List<NewsEntity> httpResultEntity) {
        adapter.setEnableLoadMore(false);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                adapter.setNewData(httpResultEntity);
                mSwipeRefresh.setRefreshing(false);
                adapter.setEnableLoadMore(true);
            }
        });
    }

    @Override
    public void loadMore(final List<NewsEntity> httpResultEntity) {
        mSwipeRefresh.setEnabled(false);
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                adapter.addData(httpResultEntity);
                adapter.loadMoreComplete();
                mSwipeRefresh.setEnabled(true);
            }
        });
    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void onRequestComplete() {

    }

    @Override
    public void onRequestError() {
        adapter.loadMoreFail();
    }

    @Override
    public void setPresenter(NewsContract.Presenter presenter) {
    }

    @Override
    public void onRefresh() {
        mPresenter.onRefresh();
    }

    @Override
    public void onLoadMoreRequested() {
        mPresenter.onLoadMore();
    }
}
